package net.onceface.core.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import net.onceface.infra.domain.AbstractEntity;

/**
 * 客户收货地址
 * **/
@Entity
@Table(name = "SYS_ADDRESS")
public class Address extends AbstractEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = 328375116565918100L;
	
	/**
	 * 用户ID
	 * **/
	@Column(name = "CLIENT_ID")
	private Long clientId;
	
	/**
	 * 收货人电话
	 * **/
	@Column(name = "PHONE")
	private String phone;
	
	/**
	 * 收货人地址
	 * **/
	@Column(name = "ADDRESS")
	private String address;
	
	
	/**
	 * 收货人姓名
	 * **/
	@Column(name = "NAME")
	private String name;
	
	/***
	 * 是否默认收货地址
	 */
	@Column(name = "DEF")
	private boolean def;
	
	/**
	 * 创建和修改时间
	 * **/
	@Column(name = "CREATE_DATE", nullable = false, updatable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createDate = new Date();

	/**
	 * 根据用户ID获取收货地址列表
	 * **/
	public static List<Address> findByClientId(Long clientId){
		return getRepository().createJpqlQuery("SELECT _address FROM Address _address WHERE _address.clientId = :clientId")
				.addParameter("clientId", clientId)
				.list();
	}
	
	/**
	 * 根据用户ID获取默认收货地址
	 * **/
	public static Address getByClientId(Long clientId){
		return getRepository().createJpqlQuery("SELECT _address FROM Address _address WHERE _address.clientId = :clientId AND _address.def = TRUE")
				.addParameter("clientId", clientId)
				.singleResult();
	}
	
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isDef() {
		return def;
	}

	public void setDef(boolean def) {
		this.def = def;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	@Override
	public String[] businessKeys() {
		// TODO Auto-generated method stub
		return null;
	}

}
